package com.example.neusoftfood_c213_zhong.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.neusoftfood_c213_zhong.R;
import com.example.neusoftfood_c213_zhong.beans.FoodInfo;
import com.example.neusoftfood_c213_zhong.beans.UserInfo;
import com.example.neusoftfood_c213_zhong.listener.RetrofitListener;
import com.example.neusoftfood_c213_zhong.model.UserModel;

import java.util.List;


public  class LoginActivity extends BaseActivity implements RetrofitListener<UserInfo> {
    private EditText etname, etpass;
    private Button btnlogin, btnregister;
    private String username = "C17310120213", password = "realmal123",user_id="";
    private Switch sw;

    private SharedPreferences sp;

    @Override
    void initViews() {
        setLayout(R.layout.activity_login);
        etname = findViewById(R.id.etuser);
        etpass = findViewById(R.id.etpass);
        btnlogin = findViewById(R.id.loginbutton);
        btnregister = findViewById(R.id.button2);
        sw = findViewById(R.id.switch1);

    }

    @Override
    void initEvents() {
        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });


        btnregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegActivity.class);
                startActivity(intent);

            }
        });

        sp =getSharedPreferences("UserInfo", Context.MODE_PRIVATE);

        readSP();


    }

    private void saveSP()
    {
        boolean remember = sw.isChecked();
        SharedPreferences.Editor editor= sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putBoolean("remember",remember);
        editor.putString("user_id",user_id);
        boolean b = editor.commit();
        Log.i("LoginActivity","----写入SP返回值"+b);
    }
    private  void readSP()
    {
        String name = sp.getString("username","");
        String pass = sp.getString("password","");
        boolean b = sp.getBoolean("remember",false);
        sw.setChecked(b);
        if(b)
        {
            etname.setText(name);
            etpass.setText(pass);
        }


    }
    void login() {
        UserModel userModel = new UserModel();
        userModel.userLogin(etname.getText().toString().trim(), etpass.getText().toString().trim(), this);
    }

    @Override
    void initData() {

    }

    @Override
    public void onSuccess(UserInfo userInfo, int flag) {

        user_id = userInfo.getUserid();
        Log.i("LoginActivity","---user_id"+user_id);
        if (!userInfo.getUserid().equals("0")) {
            getUser_id();
            getUser_password();
            saveSP();
            Toast.makeText(LoginActivity.this, "登陆成功 ", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        } else
        {
            Toast.makeText(LoginActivity.this, "用户名或密码错误 ", Toast.LENGTH_SHORT).show();
        }

    }


    @Override
    public void onFail() {
        Toast.makeText(LoginActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
    }




}